{include file="common/pc/header" title="$enterpriseModule.module.title - API设置" /}

<style>
    .layui-form-checkbox i {
        height: 26px;
    }
    .module-api .layui-table-body {
        overflow-x: hidden;
    }
</style>

<!-- API权限设置 start -->

<div class="module-api">
    <form class="layui-form" lay-filter="apis">
        <table class="layui-hide" id="apis"></table>
    </form>
</div>

<script type="text/html" id="apisToolBarTpl">
    <input type="checkbox" name="apiStatus" value="{{d.name}}" lay-filter="apiStatus" title="允许调用"
           {{d.status== 'allow' ? 'checked' : '' }}
    />
    {{#  if(JSON.stringify(d.privacyFields) != '{}' && JSON.stringify(d.privacyFields) != '[]'){ }}
    &nbsp;&nbsp;
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-filter="apiPrivacyFields" data-api="{{d.name}}">
        隐私设置
    </button>
    <form class="layui-form" lay-filter="privacyFieldsForm{{d.name}}">
        <table class="layui-table" style="margin: 10px;width: 500px;text-align: center;">
            <thead>
            <tr>
                <th style="text-align: center;">字段名称</th>
                <th style="text-align: center;">字段介绍</th>
                <th style="text-align: center;">是否允许访问</th>
            </tr>
            </thead>
            <tbody>
            {{# layui.each(d.privacyFields, function(index, item){ }}
            <tr>
                <td>{{index}}</td>
                <td>{{item.desc}}</td>
                <td>
                    <input type="checkbox" lay-skin="switch" lay-text="允许|禁止"
                           value="{{index}}"
                           {{ item.status== 'allow' ? 'checked':'' }}
                    />
                </td>
            </tr>
            {{# }); }}
            </tbody>
        </table>
    </form>
    {{#  } }}
</script>

<script>
    var rows = [];
</script>
<?php
foreach($apis as $api) {
$privacyFields = json_encode($api['privacyFields']);
?>
<script>
    rows.push({
        'name': '{$api.api}',
        'title': '{$api.title}',
        'desc': '{$api.desc}',
        'status': '{$api.status}',
        'privacyFields': {$privacyFields}
    });
</script>
<?php
}
?>

<script>
    layui.use(['table', 'form', 'layer'], function () {
        var table = layui.table
            , form = layui.form
            , layer = layui.layer;

        var tableWidth = (window.frameElement ? $(window.frameElement).width() : $(window).width());
        //渲染表格
        table.render({
            elem: '#apis'
            , cellMinWidth: 150
            , cols: [[ //标题栏
                {field: 'name', fixed: true, title: 'api名称', width: 390}
                , {field: 'title', fixed: true, title: 'api标题', width: 300}
                , {field: 'desc', title: 'api介绍'}
                , {
                    field: 'toolbar',
                    templet: '#apisToolBarTpl',
                    width: 220,
                    unresize: true,
                    title: '安全设置'
                }
            ]]
            , data: rows
            , limit: rows.length
            , even: true
            , width: tableWidth
        });
        //toolbar：api是否启用
        form.on('checkbox(apiStatus)', function (obj) {
            status = obj.elem.checked ? "allow" : "deny";
            $("i", obj.othis).removeClass("layui-icon-ok").addClass("layui-icon-loading layui-checkbox-icon-loading");
            $.post("/core/enterprise.module/updateApiStatus?mid={$mid}", {
                "status": status,
                "apiName": obj.value
            }, function (ret) {
                $("i", obj.othis).addClass("layui-icon-ok").removeClass("layui-icon-loading layui-checkbox-icon-loading");
                if (ret.code == 0) {
                    //失败
                    obj.elem.checked = !obj.elem.checked;
                    layer.tips(ret.msg, obj.othis);
                    form.render("checkbox", "apis");
                } else {
                    layer.msg(ret.msg);
                }
            }, "json");
        });
        //toolbar：隐私字段设置
        $("button[lay-filter='apiPrivacyFields']").click(function () {
            event.preventDefault();
            var apiName = $(this).attr("data-api");
            var $formElem = $("form[lay-filter='privacyFieldsForm" + apiName + "']");
            var btnElem = this;
            var height = $(window.frameElement).height() - 50;
            layer.open({
                type: 1
                , title: "隐私字段设置"
                , closeBtn: false
                , area: ['520px', height + 'px']
                , shade: 0.8
                , id: 'LAY_apiPrivacyFields' //设定一个id，防止重复弹出
                , btn: ['确定', '取消']
                , btnAlign: 'c'
                , moveType: 1 //拖拽模式，0或者1
                , content: $formElem
                , success: function (layero) {
                    var btn = layero.find('.layui-layer-btn');
                    btn.find('.layui-layer-btn0').click(function () {
                        var fields = [];
                        $("input[type='checkbox']", $formElem).each(function () {
                            if (this.checked) {
                                fields.push(this.value);
                            }
                        });
                        $.post("/core/enterprise.module/updatePrivacyFields?mid={$mid}", {
                            "fields": fields.join(","),
                            "apiName": apiName
                        }, function (ret) {
                            if (ret.code == 0) {
                                //失败
                                layer.tips(ret.msg, btnElem);
                            } else {
                                layer.msg(ret.msg);
                            }
                        }, "json");
                    });
                }
            });
        });
    });
</script>
<!-- API权限设置 end -->

{include file="common/pc/footer"/}